package com.directv.z8training.EMS.DAO;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.directv.z8training.EMS.Entity.Group;
import com.directv.z8training.EMS.Entity.Project;
import com.directv.z8training.EMS.Entity.ProjectUser;
import com.directv.z8training.EMS.Entity.ProjectUserPK;
import com.directv.z8training.EMS.Entity.User;

@Repository
public class UserDAOImpl implements UserDAO {

	@PersistenceContext
	private EntityManager entityManager;

	@Override
	public User checkLogin(String username, String password) {
		// TODO Auto-generated method stub
		Query query = entityManager
				.createQuery("SELECT u FROM User u WHERE u.username=:username AND u.password=:password");
		query.setParameter("username", username);
		query.setParameter("password", password);
		User user;
		try {
			user = (User) query.getSingleResult();
		} catch (NoResultException e) {
			return null;
		}
		return user;
	}

	@Override
	public void createPM(String username, String password, String fullname,
			String address, String phone, String code) {
		// TODO Auto-generated method stub
		User user = new User();
		user.setUsername(username);
		user.setPassword(password);
		user.setFullname(fullname);
		user.setAddress(address);
		user.setPhone(phone);
		Group group = new Group();
		group.setCode(code);
		user.setGroup(group);
		entityManager.persist(user);

	}

	@Override
	public void createDEV(String username, String password, String fullname,
			String address, String phone, String projectcode) {
		// TODO Auto-generated method stub
		User user = new User();
		user.setUsername(username);
		user.setPassword(password);
		user.setFullname(fullname);
		user.setAddress(address);
		user.setPhone(phone);
		// Set Group
		Group group = new Group();
		group.setCode("DEV");
		user.setGroup(group);
		// Set project code
		List<ProjectUser> projectUsers = new ArrayList<ProjectUser>();
		ProjectUser proUser = new ProjectUser();
		ProjectUserPK proUserPK = new ProjectUserPK();
		proUserPK.setUsername(username);
		proUserPK.setProjectcode(projectcode);

		proUser.setId(proUserPK);
		projectUsers.add(proUser);
		user.setProjectUsers(projectUsers);

		entityManager.persist(user);

	}

	@Override
	public void changePassword(User user, String password) {
		user.setPassword(password);
		entityManager.merge(user);
	}

	@Override
	public void removeDEV(String username, String projectCode) {
		User user = findUser1(username);
		List<ProjectUser> list = user.getProjectUsers();
		
		ProjectUser projectUser = new ProjectUser();
		for(ProjectUser p: list){
			if(user.getUsername().equals(p.getUser().getUsername())&&
					p.getProject().getCode().equals(projectCode)){
				projectUser = p;
			}
		}
		Project project = projectUser.getProject();
		user.getProjectUsers().remove(projectUser);
		project.getProjectUsers().remove(projectUser);
	}

	@Override
	public User findUser1(String username) {
		// TODO Auto-generated method stub

		Query query = entityManager
				.createQuery("SELECT u FROM User u WHERE u.username=:username");
		query.setParameter("username", username);
		User user;
		try {
			user = (User) query.getSingleResult();
		} catch (NoResultException e) {
			return null;
		}
		return user;

	}

	@Override
	public User findUserByUserName(String username) {
		return entityManager.find(User.class, username);
	}

	@Override
	@Transactional
	public void updateInformation(User user, String fullname, String address, String phone) {
		user.setFullname(fullname);
		user.setAddress(address);
		user.setPhone(phone);
		entityManager.merge(user);
	}

	@Override
	public List<ProjectUser> findListProjectUserByUsername(User user) {
		// TODO Auto-generated method stub
		List<ProjectUser> projectUser = user.getProjectUsers();
		Project project = new Project();
		for (ProjectUser pu : projectUser) {
			project = pu.getProject();
			//model.addObject("project", project.getProjectUsers());
		}
		return project.getProjectUsers();
	}
	
	@Override
	public Project findProjectUserByCode(String code) {
		// TODO Auto-generated method stub
		return entityManager.find(Project.class, code);
	}

	@Override
	public List<User> findAllUser() {
		// TODO Auto-generated method stub
		List<User> users = entityManager.createNamedQuery("User.findAll", User.class).getResultList();
		return users;
	}

	@Override
	public List<User> findListUnprojectUser() {
		// TODO Auto-generated method stub
		List<User> listUsers = findAllUser();
		List<User> listUnprojectUsers = new ArrayList<User>();
		for (User user : listUsers) {
			if (user.getProjectUsers().isEmpty() && user.getGroup().getCode().equals("DEV")) {
				listUnprojectUsers.add(user);
			}
		}
		return listUnprojectUsers;
	}

	@Override
	public void assignDEVToProject(String username, String projectCode) {
		// TODO Auto-generated method stub
		List<ProjectUser> projectUsers = new ArrayList<ProjectUser>();
		ProjectUser proUser = new ProjectUser();
		ProjectUserPK proUserPK = new ProjectUserPK();
		proUserPK.setUsername(username);
		proUserPK.setProjectcode(projectCode);

		proUser.setId(proUserPK);
		projectUsers.add(proUser);
//		User user = findUserByUserName(username);
//		user.setProjectUsers(projectUsers);
		entityManager.persist(proUser);
	}
	
	@Override
	public boolean checkAvailable(String username){
		User user = findUser1(username);
		List<ProjectUser> list = user.getProjectUsers();
		Date current = new Date();
		for(ProjectUser p: list){
			if(p.getProject().getEnddate() == null || p.getProject().getEnddate().compareTo(current)>0){
				return false;
			}
		}
		return true;
	}
	
}
